import { login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getProfileAPI } from '@/api/user'

const state = {
  token: getToken() || null
}
const mutations = {
  // 存储token
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  // 删除token
  removeToken(state) {
    state.token = null
    removeToken()
  }

}
const actions = {
  async login(context, payload) {
    const { username, password } = payload
    const { data: { token }} = await login(username, password)
    context.commit('setToken', token)
  },
  async getProfile(ctx) {
    const res = await getProfileAPI()
    ctx.commit('setProfile', res.data)
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

